package com.ciwong.epaper.modules.bookstore.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.clazz.bean.StudentBean;
import com.ciwong.epaper.modules.bookstore.bean.StudentGroupBean;
import com.ciwong.epaper.modules.me.ui.MembersActivity;
import com.ciwong.mobilelib.widget.NoScrollGridView;

import java.util.List;

public class MembersAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    //组名
    private String teamName;

    private String grouping = "编辑";

    private GridView gridView;
    //小组集合
    private List<StudentGroupBean> studentBeans;

    private StuGridViewAdapter stuGridViewAdapter;


    public MembersAdapter(Context context, List<StudentGroupBean> studentBeans) {
        this.mContext = context;
        this.studentBeans = studentBeans;
    }

    @Override
    public int getGroupCount() {
        return studentBeans.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return 1;
    }


    @Override
    public Object getGroup(int i) {
        return studentBeans.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return studentBeans.get(i).getList().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        final ViewHolderGroup groupHolder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.activity_stu_class_parent, viewGroup, false);
            groupHolder = new ViewHolderGroup();
            groupHolder.stu_class_group_text = view.findViewById(R.id.stu_class_group_text);
            groupHolder.stu_class_radBtn = view.findViewById(R.id.stu_class_radBtn);
            view.setTag(groupHolder);
        } else {
            groupHolder = (ViewHolderGroup) view.getTag();
        }
        if (studentBeans.get(i).getCheckCount() == studentBeans.get(i).getList().size()) {
            groupHolder.stu_class_radBtn.setChecked(true);
        } else {
            groupHolder.stu_class_radBtn.setChecked(false);
        }
        groupHolder.stu_class_radBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!compoundButton.isPressed()) {
                    return;
                }
                if (b) {
                    groupHolder.stu_class_radBtn.setChecked(true);
                    studentBeans.get(i).setCheckCount(studentBeans.get(i).getList().size());
                    List<StudentBean> studentBeanList = studentBeans.get(i).getList();
                    for (int i1 = 0; i1 < studentBeanList.size(); i1++) {
                        studentBeanList.get(i1).setCheckStatus(1);
                    }
                    MembersAdapter.this.notifyDataSetChanged();
                    if (mContext instanceof MembersActivity) {
                        ((MembersActivity) mContext).setChooseStuCount();
                    }
                } else {
                    groupHolder.stu_class_radBtn.setChecked(false);
                    studentBeans.get(i).setCheckCount(0);
                    List<StudentBean> studentBeanList = studentBeans.get(i).getList();
                    for (int i1 = 0; i1 < studentBeanList.size(); i1++) {
                        studentBeanList.get(i1).setCheckStatus(2);
                    }
                    MembersAdapter.this.notifyDataSetChanged();
                    if (mContext instanceof MembersActivity) {
                        ((MembersActivity) mContext).setChooseStuCount();
                    }
                }
            }
        });
        teamName = studentBeans.get(i).getList().get(0).getTeamName();
        if ("".equals(teamName) || null == teamName) {
            teamName = "未分组";
            grouping = "分组";
        }
        groupHolder.stu_class_group_text.setText(teamName);
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        if (null == view) {
            view = LayoutInflater.from(mContext).inflate(R.layout.activity_stu_class_grid, viewGroup, false);
        }
        gridView = (NoScrollGridView) view;
        stuGridViewAdapter = new StuGridViewAdapter(mContext, studentBeans.get(i), MembersAdapter.this);
        gridView.setAdapter(stuGridViewAdapter);

        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    private static class ViewHolderGroup {
        private TextView stu_class_group_text;
        private TextView stu_class_grouping;
        private CheckBox stu_class_radBtn;
    }
}
